using System.IO;
using System.Web.UI;

using RealDolmen.Helpers;

namespace RealDolmen.Controls
{
    public class FileVersionControl : UserControl
    {
        #region Properties
        private bool showInHtmlComment = true;

        public bool ShowInHtmlComment
        {
            get { return showInHtmlComment; }
            set { showInHtmlComment = value; }
        }

        private string assembly;

        public string Assembly
        {
            get { return assembly; }
            set { assembly = value; }
        } 
        #endregion // Properties


        #region Overrides
        protected override void Render(HtmlTextWriter writer)
        {
            if (ShowInHtmlComment)
            {
                writer.Write("<!-- ");
            }

            if (!string.IsNullOrEmpty(Assembly))
            {
                System.Reflection.Assembly assembly =
                    System.Reflection.Assembly.Load(Assembly);
                if (assembly != null)
                {
                    string name = Path.GetFileName(assembly.Location);
                    string version = Helper.GetAssemblyFileVersion(assembly);
                    writer.Write("File version of {0}: {1}", name, version);
                }
            }

            if (ShowInHtmlComment)
            {
                writer.WriteLine(" -->");
            }
        } 
        #endregion // Overrides
    }
}
